package com.com.unionpay.speech.receiver;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.com.unionpay.constant.Constant;
import com.sdk.ActiveMode;
import com.sdk.LogUtils;
import com.sdk.MessageEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Receives broadcasted intents. In particular, we are interested in the
 * android.media.AUDIO_BECOMING_NOISY and android.intent.action.MEDIA_BUTTON
 * intents, which is broadcast, for example, when the user disconnects the
 * headphones. This class works because we are declaring it in a
 * &lt;receiver&gt; tag in AndroidManifest.xml.
 */
@SuppressLint("InlinedApi")
public class BluetoothReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "MusicIntentReceiver";
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Log.i(LOG_TAG, "ccACTION_MEDIA_BUTTON!");
        if (intent.getAction().equals(
                android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
            Toast.makeText(context, "Headphones disconnected.",
                    Toast.LENGTH_SHORT).show();

            // send an intent to our MusicService to telling it to pause the
            // audio
            // context.startService(new Intent(MusicService.ACTION_PAUSE));

        } else if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
            Log.i(LOG_TAG, "ACTION_MEDIA_BUTTON!");
            KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(
                    Intent.EXTRA_KEY_EVENT);
            Log.e(LOG_TAG,"keycode="+keyEvent.getKeyCode());
            Log.e(LOG_TAG,"keyevent="+keyEvent);
            if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
                return;
            switch (keyEvent.getKeyCode()) {
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    LogUtils.e("消息已发送");
                    EventBus.getDefault().post(new MessageEvent(Constant.EVENT_BLUETOOTH_ACTIVE, ActiveMode.BLUETOOTH));
                    // context.startService(new
                    // Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                    // context.startService(new Intent(MusicService.ACTION_PLAY));
                    LogUtils.e("消息已发送");
                    EventBus.getDefault().post(new MessageEvent(Constant.EVENT_BLUETOOTH_ACTIVE,  ActiveMode.BLUETOOTH));
                    break;
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                    LogUtils.e("消息已发送");
                    EventBus.getDefault().post(new MessageEvent(Constant.EVENT_BLUETOOTH_ACTIVE,  ActiveMode.BLUETOOTH));
                    // context.startService(new Intent(MusicService.ACTION_PAUSE));
                    break;
                case KeyEvent.KEYCODE_MEDIA_STOP:
                    // context.startService(new Intent(MusicService.ACTION_STOP));
                    break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    // context.startService(new Intent(MusicService.ACTION_SKIP));
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    // TODO: ensure that doing this in rapid succession actually
                    // plays the
                    // previous song
                    // context.startService(new Intent(MusicService.ACTION_REWIND));
                    break;
            }
        } else if (intent.getAction().equals(
                BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
            Log.i(LOG_TAG, "BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED");

            int state = intent.getIntExtra(
                    BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);

            if (state == BluetoothAdapter.STATE_CONNECTED
                    || state == BluetoothAdapter.STATE_DISCONNECTED) {
            }
        } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.i(LOG_TAG, "Intent.ACTION_BOOT_COMPLETED");
        } else if (intent.getAction().equals("android.intent.action.UPDATE_SUSPEND_TIME_BY_HAND")) {
            Log.i(LOG_TAG, "Intent.UPDATE_SUSPEND_TIME_BY_HAND");
        } else {
            Log.i(LOG_TAG, "other intent");
        }
    }

}
